//
//  ServiceViewController.m
//  JimiPay
//
//  Created by yull on 16/1/6.
//  Copyright (c) 2016年 com.shengyun. All rights reserved.
//

#import "ServiceViewController.h"
#import "LoginViewController.h"
#import "RateViewController.h"
#import "AboutUsViewController.h"
#import "FAQViewController.h"

#import "UIAlertView+BlocksKit.h"

@interface ServiceViewController ()

@property (nonatomic, strong) NSArray *imageArray;
@property (nonatomic, strong) NSArray *titleArray;

@end

@implementation ServiceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.navigationItem.title = @"服务台";
    [self setupShareRightItem];
    [self setupValidateLeftItem:[[TDUser defaultUser].custStatus isEqualToString:@"2"]];
    
    self.imageArray = @[@"service_icon1",
                        @"service_icon2",
                        @"service_icon3",
                        @"service_icon4",
                        @"service_icon5",
                        @"service_icon3"];
    self.titleArray = @[@"费率说明",
                        @"常见问题",
                        [NSString stringWithFormat:@"版本号：%@", [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"]],
                        [NSString stringWithFormat:@"客服电话：%@", KEFU_PNONENUM],
                        [NSString stringWithFormat:@"客服QQ：%@", KEFU_QQ],
                        @"关于我们"];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"ServiceCell"];
    
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 1, CGRectGetWidth(self.tableView.frame), 80)];
    footerView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    UIButton *logoutB = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    logoutB.frame = CGRectMake(10, 16, CGRectGetWidth(self.tableView.frame) - 20, 44);
    logoutB.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    logoutB.backgroundColor = MainColor;
    [logoutB setTitle:@"安全退出" forState:0];
    [logoutB setTitleColor:[UIColor whiteColor] forState:0];
    logoutB.layer.cornerRadius = 5;
    logoutB.layer.masksToBounds = YES;
    [logoutB addTarget:self action:@selector(button_logout:) forControlEvents:UIControlEventTouchUpInside];
    [footerView addSubview:logoutB];
    
    self.tableView.tableFooterView = footerView;
}

- (void)button_logout:(id)sender {
    
    UIAlertView *alerView = [UIAlertView bk_alertViewWithTitle:@"确定退出？"];
    [alerView bk_setCancelButtonWithTitle:@"取消" handler:^{
        
    }];
    [alerView bk_addButtonWithTitle:@"确定" handler:^{
        
        LoginViewController *loginVC = [[LoginViewController alloc] init];
        UINavigationController *loginNav = [[UINavigationController alloc] initWithRootViewController:loginVC];
        [UIApplication sharedApplication].delegate.window.rootViewController = loginNav;
    }];
    [alerView show];
}

- (void)checkVersion
{
    [TDHttpEngine requestFor_CHECKVERSION_WithCustId:[[NSUserDefaults standardUserDefaults] objectForKey:CUST_ID] custMobile:[[NSUserDefaults standardUserDefaults] objectForKey:CUST_MOBILE] appVersion:[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"] appPlatform:@"1" complete:^(BOOL succeed, NSString *msg, id reqBody) {
        if (succeed) {
            NSInteger checkState = [reqBody[@"checkState"] integerValue];
            NSString *fileDesc = reqBody[@"fileDesc"];
            NSString *fileUrl = reqBody[@"fileUrl"];
            if (checkState == 1 || checkState == 2) {
                UIAlertView *alertView = [UIAlertView bk_alertViewWithTitle:@"有新版本更新" message:fileDesc];
                [alertView bk_addButtonWithTitle:@"更新" handler:^{
                    [[UIApplication  sharedApplication] openURL:[NSURL URLWithString:fileUrl]];
                    if (checkState == 2) {
                        exit(0);
                    }
                }];
                
                if (checkState == 1) {
                    [alertView bk_setCancelButtonWithTitle:@"取消" handler:^{
                        
                    }];
                }
                
                [alertView show];
            } else {
                [self.view makeToast:@"亲！已经是最新版本了！" duration:2.0f position:@"center"];
            }
        }
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.imageArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ServiceCell"];
    
    cell.imageView.image = [UIImage imageNamed:self.imageArray[indexPath.row]];
    cell.textLabel.text = self.titleArray[indexPath.row];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.row == 0) {
        
        RateViewController *controller = [[RateViewController alloc] init];
        controller.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:controller animated:YES];
    } else if (indexPath.row == 1) {
        
        FAQViewController *controller = [[FAQViewController alloc] init];
        controller.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:controller animated:YES];
    } else if (indexPath.row == 2) {
        
        [self checkVersion];
    } else if (indexPath.row == 3) {
        
        UIAlertView *alerView = [UIAlertView bk_alertViewWithTitle:APPNAME message:[NSString stringWithFormat:@"服务热线：%@", KEFU_PNONENUM]];
        [alerView bk_setCancelButtonWithTitle:@"取消" handler:^{
            
        }];
        [alerView bk_addButtonWithTitle:@"拨打客服电话" handler:^{
            
            NSString *deviceType = [UIDevice currentDevice].model;
            if([deviceType  isEqualToString:@"iPod touch"]||[deviceType  isEqualToString:@"iPad"]||[deviceType  isEqualToString:@"iPhone Simulator"]){
                NSLog(@"该设备不可拨打电话");
                [self.view makeToast:@"该设备不可拨打电话" duration:1.5f position:@"center"];
                return;
            }
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel://%@", KEFU_PNONENUM]]];
        }];
        [alerView show];
    } else if (indexPath.row == 4) {
        
    } else if (indexPath.row == 5) {
        
        AboutUsViewController *controller = [[AboutUsViewController alloc] init];
        controller.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:controller animated:YES];
    }
}

@end
